﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：数据层辅助类；
  //创建人：赵国伟
  //生成时间：2012/04/03 12:37:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
using Shopping.Model;
using Shopping.Common;
using System.Reflection;
using System.Data;

namespace Shopping.DAL
{
    public class DALHelper
    {


        #region "执行查询 返回基本信息对象结构； by 赵国伟 2012-04-03"
        /// <summary>
        /// 执行查询 返回基本信息对象结构；
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static List<IDNameObj> QueryIDNameObjs(string sql)
        {
            List<IDNameObj> listOfData = null;
            using (SqlDataReader reader = DbHelperSQL.ExecuteReader(sql))
            {
                if (reader.HasRows)
                {
                    listOfData = new List<IDNameObj>();
                    while (reader.Read())
                    {
                        IDNameObj data = new IDNameObj();
                        data.ID = int.Parse(reader[0].ToString());
                        data.Name = reader[1].ToString();
                        listOfData.Add(data);
                    }
                }
            }
            return listOfData;
        }

        public static List<IDNameObj> QueryIDNameObjs<T>(string cond,Enum objEnum)
            where T : class  //限制该泛型必须为引用类型；
        {
            return QueryIDNameObjs<T>(cond, "", objEnum);
        }
        public static List<IDNameObj> QueryIDNameObjs<T>(string cond,string orderby, Enum objEnum)
           where T : class  //限制该泛型必须为引用类型；
        {
            Type objClass = typeof(T);
            string pkFiled = objClass.GetProperties()[0].Name;
            string tableName = objClass.Name;
            string sql = String.Format("Select {0},{1} from {2}  ", pkFiled, objEnum.ToString(), tableName);
            if (!string.IsNullOrEmpty(cond))
                sql += " Where " + cond;
            if (!string.IsNullOrEmpty(orderby))
                sql += " Order by " + orderby;
            return QueryIDNameObjs(sql);
        }


        public static IDNameObj QueryIDNameObj(string sql)
        {
            IDNameObj data = null;
            using (SqlDataReader reader = DbHelperSQL.ExecuteReader(sql))
            {
                if (reader.HasRows)
                {
                    data = new IDNameObj();
                    while (reader.Read())
                    {
                        data.ID = int.Parse(reader[0].ToString());
                        data.Name = reader[1].ToString();
                    }
                }
            }
            return data;
        }
        #endregion

        #region "查询指定字段的值；" by 赵国伟 2012-04-03
        /// <summary>
        /// 查询指定字段的值；
        /// </summary>
        /// <returns></returns>
        public static string GetFieldValue<T>(int keyId,string fieldName)
            where T : class  //限制该泛型必须为引用类型；
        {
            Type objClass = typeof(T);
            string pkFiled = objClass.GetProperties()[0].Name; 
            string sql = string.Format("Select {0} from {1} Where {2}={3}", fieldName, objClass.Name,pkFiled, keyId);
            return DbHelperSQL.GetSingle(sql) as string;
        }
        #endregion

        #region "设置指定表的状态字段； by 赵国伟 2012-04-04"
        /// <summary>
        /// 设置指定表的状态字段；
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="keyId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static int SetTableStatus<T>(int keyId, string status)
            where T : class  //限制该泛型必须为引用类型；
        {
            Type objClass = typeof(T);
            string pkFiled = objClass.GetProperties()[0].Name;
            string sql = string.Format("Update {0} Set Status='{1}' Where {2}={3}", objClass.Name, status, pkFiled, keyId);
            return DbHelperSQL.ExecuteSql(sql);
        }
        #endregion

        #region "设置指定表的状态字段； by 赵国伟 2012-04-04"
        /// <summary>
        /// 设置指定表的状态字段；
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="keyId"></param>
        /// <param name="fieldEnum">字段枚举</param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static int SetFieldValue<T>(int keyId, Enum fieldEnum, string fieldValue)
            where T : class  //限制该泛型必须为引用类型；
        {
            Type objClass = typeof(T);
            string pkFiled = objClass.GetProperties()[0].Name;
            string sql = string.Format("Update {0} Set {1}='{2}' Where {3}={4}", objClass.Name,fieldEnum.ToString(),fieldValue, pkFiled, keyId);
            return DbHelperSQL.ExecuteSql(sql);
        }
        #endregion

        #region "执行SQL返回DataTable by 赵国伟 2012-04-18"
        /// <summary>
        /// 执行SQL返回DataTable
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataTable Query(string sql)
        {
            DataSet ds = DbHelperSQL.Query(sql);
            if(ds != null)
                return ds.Tables[0];
            return null;
        }
        #endregion

    }
}
